﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Data;
using Newtonsoft.Json;

namespace NetJsWire.Direct.customJsonConverter
{
    class DataRowArrayConverter : JsonConverter
    {
        /// <summary>
        ///   Writes the JSON representation of the object.
        /// </summary>
        /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
        /// <param name = "value">The value.</param>
        public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
        {
            var rows = value as DataRowCollection;

            

            // *** HACK: need to use root serializer to write the column value
            //     should be fixed in next ver of JSON.NET with writer.Serialize(object)
            var ser = new JsonSerializer();
            writer.WriteStartArray();
            for (var i = 0; i < rows.Count; i++)
            {
               

                var row = rows[i];
                

                writer.WriteStartObject();
                foreach (DataColumn column in row.Table.Columns)
                {
                    writer.WritePropertyName(column.ColumnName);
                    ser.Serialize(writer, row[column]);
                }
                writer.WriteEndObject();
            }
            writer.WriteEndArray();
        }


        /// <summary>
        ///   Determines whether this instance can convert the specified value type.
        /// </summary>
        /// <param name = "valueType">Type of the value.</param>
        /// <returns>
        ///   <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanConvert(Type valueType)
        {
            return typeof(DataRowCollection).IsAssignableFrom(valueType);
        }

        /// <summary>
        ///   Reads the JSON representation of the object.
        /// </summary>
        /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
        /// <param name = "objectType">Type of the object.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
        {
            throw new NotImplementedException();
        }
    }

    
}
